package com.king.tv.mvp.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.king.tv.R;
import com.king.tv.bean.LiveInfo;
import com.king.tv.bean.Video;
import com.king.tv.bean.VideoBean;
import com.king.tv.http.ApiConfig;
import com.king.tv.util.RoundedCornersTransformation2;

import java.util.List;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
 * @since 2017/3/3
 */

public class EasyLiveAdapter extends RecyclerArrayAdapter<Video> {

    private boolean isShowStatus;

    private Context context;
    public EasyLiveAdapter(Context context, List<Video> objects,boolean isShowStatus) {
        super(context, objects);
        this.isShowStatus = isShowStatus;
        this.context = context;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new LiveViewHolder(parent);
    }

    public class LiveViewHolder extends BaseViewHolder<Video>{


        ImageView iv;
        TextView tvTitle;
        TextView tvStatus;
        TextView tvName;
        TextView tvViewer;
        ImageView ivitemlive;
        ImageView ivtx;


        public LiveViewHolder(ViewGroup parent) {
            super(parent,R.layout.list_live_item);
            iv = $(R.id.iv);
            tvTitle = $(R.id.tvTitle);
            tvStatus = $(R.id.tvStatus);
            tvName = $(R.id.tvName);
            tvViewer = $(R.id.tvViewer);
            ivtx =  $(R.id.imageView);
            ivitemlive = $(R.id.ivitemlive);
        }

        @Override
        public void setData(Video data) {
            super.setData(data);
            //设置图片圆角角度
            Log.i("tag","=============url"+data.getUser());
            if(!TextUtils.isEmpty(data.getImg())){
                String url = ApiConfig.BASE_URL+data.getImg();
                Glide.with(getContext()).load(url).error(R.mipmap.live_default).transform(new RoundedCornersTransformation2(3, 0, RoundedCornersTransformation2.CornerType.ALL, RoundedCornersTransformation2.ScaleType.CENTER_CROP)).diskCacheStrategy(DiskCacheStrategy.RESOURCE).into(iv);
            }else{
                Glide.with(getContext()).load(R.mipmap.live_default).transform(new RoundedCornersTransformation2(3, 0, RoundedCornersTransformation2.CornerType.ALL, RoundedCornersTransformation2.ScaleType.CENTER_CROP)).diskCacheStrategy(DiskCacheStrategy.RESOURCE).into(iv);
            }
            try {
                if(!TextUtils.isEmpty(data.getUser().getPic())){
                    String picurl = ApiConfig.BASE_URL+data.getUser().getPic();
                    Log.i("tag","==========txpic"+picurl);
                    Glide.with(getContext()).load(picurl).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(ivtx);
                }else{
                    Glide.with(getContext()).load(R.drawable.img_zhibojian_shu_zhubo_gerenziliao).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(ivtx);
                }
            } catch (Exception e) {
                Log.i("tag","=============url"+e);
                e.printStackTrace();
            }
//            tvTitle.setText(data.getTypeNote());
//            tvName.setText(data.getTitle());
            int num = (int)(1+Math.random()*(2000));
            tvViewer.setText(num+"");
            Video.UserBean userBean = data.getUser();
            if(userBean!=null){
                tvTitle.setText(userBean.getNickname());
                tvName.setText(data.getTitle());
                Glide.with(getContext()).load(R.mipmap.ic_launcher).diskCacheStrategy(DiskCacheStrategy.RESOURCE).into(ivitemlive);
            }
//            tvViewer.setText(data.getViews());

//            if(isShowStatus){
//                if(data.getPlay_status()){
//                    tvStatus.setVisibility(View.VISIBLE);
//                }else{
//                    tvStatus.setVisibility(View.GONE);
//                }
//            }

        }
    }
    public  int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
